package ee.maoinvest.backend.services.implementations;

import java.sql.Connection;
import java.sql.Date;

import org.apache.log4j.Logger;

import ee.maoinvest.backend.database.ConnectionManager;
import ee.maoinvest.backend.model.dao.EnterpriseDAO;
import ee.maoinvest.backend.model.dao.LatestDatesDAO;
import ee.maoinvest.backend.model.dao.factory.DAOFactory;
import ee.maoinvest.backend.services.LatestDatesService;

public class LatestDatesServiceImpl implements LatestDatesService
{
	Logger logger = Logger.getLogger(LatestDatesServiceImpl.class);
	DAOFactory daoFactory = DAOFactory.getDAOFactory();
	
	private int INSERTION_WAS_SUCCESSFUL = 1;
	private int INSERTION_WAS_NOT_SUCCESSFUL = 0;
	private int CODE_PRODUCED_AN_ERROR = -1;
	
	private int UPDATE_WAS_SUCCESSFUL = 1;
	private int UPDATE_WAS_NOT_SUCCESSFUL = 0;
	
	
	public Date getDateDefaultEnterpriseWasInsertedIntoSystem()
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		EnterpriseDAO enterpriseDAOImpl = daoFactory.getEnterpriseDAOImpl(dbConnection);
		Date dateRetrieved = null;
		
		try 
		{
			dateRetrieved = enterpriseDAOImpl.getDateDefaultEnterpriseWasInsertedIntoSystem();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeConnection();
		}
		
		return dateRetrieved;
	}


	@Override
	public Date getLatestDateWhileInsertingNewContractBetweenEnterpriseAndElectricityMeter(
			int contractID, int enterpriseID, int electricityMeterID) 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		LatestDatesDAO latestDatesDAOImpl = daoFactory.getLatestDatesDAOImpl(dbConnection);
		Date dateRetrieved = null;
		
		try 
		{
			dateRetrieved = latestDatesDAOImpl.getLatestDateWhileInsertingNewContractBetweenEnterpriseAndElectricityMeter(contractID, enterpriseID, electricityMeterID);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeConnection();
		}
		
		return dateRetrieved;
	}


	@Override
	public Date getLatestDateWhileInsertingSubstationMainElectricityMeter(
			int substationID, int transformerCoefficentStandardID)
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		LatestDatesDAO latestDatesDAOImpl = daoFactory.getLatestDatesDAOImpl(dbConnection);
		Date dateRetrieved = null;
		
		try 
		{
			dateRetrieved = latestDatesDAOImpl.getLatestDateWhileInsertingSubstationMainElectricityMeter(substationID, transformerCoefficentStandardID);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeConnection();
		}
		
		return dateRetrieved;
	}


	@Override
	public Date getLatestDateWhileInsertingLocalTerritoryElectricityMeter(
			int parentElectricityMeterID, int transformerCoefficentStandardID) 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		LatestDatesDAO latestDatesDAOImpl = daoFactory.getLatestDatesDAOImpl(dbConnection);
		Date dateRetrieved = null;
		
		try 
		{
			dateRetrieved = latestDatesDAOImpl.getLatestDateWhileInsertingLocalTerritoryElectricityMeter(parentElectricityMeterID, transformerCoefficentStandardID);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeConnection();
		}
		
		return dateRetrieved;
	}
	
	@Override
	public Date getLatestDateWhileInsertingOuterTerritoryElectricityMeter(
			int electricityContractID, int transformerCoefficentStandardID)
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		LatestDatesDAO latestDatesDAOImpl = daoFactory.getLatestDatesDAOImpl(dbConnection);
		Date dateRetrieved = null;
		
		try 
		{
			dateRetrieved = latestDatesDAOImpl.getLatestDateWhileInsertingOuterTerritoryElectricityMeter(electricityContractID, transformerCoefficentStandardID);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeConnection();
		}
		
		return dateRetrieved;
	}
	
	@Override
	public Date getLatestDateWhileDeletingElectricityMeterParentMeterIncluded(
			int electricityMeterIDChosenToDelete, int parentMeterIDOfMeterChosenToDelete) 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		LatestDatesDAO latestDatesDAOImpl = daoFactory.getLatestDatesDAOImpl(dbConnection);
		Date dateRetrieved = null;
		
		try 
		{
			dateRetrieved = latestDatesDAOImpl.getLatestDateWhileDeletingElectricityMeterParentMeterIncluded(electricityMeterIDChosenToDelete, parentMeterIDOfMeterChosenToDelete);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeConnection();
		}
		
		return dateRetrieved;
	}
	
	@Override
	public Date getLatestDateWhileDeletingElectricityMeter(int electricityMeterIDChosenToDelete) 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		LatestDatesDAO latestDatesDAOImpl = daoFactory.getLatestDatesDAOImpl(dbConnection);
		Date dateRetrieved = null;
		
		try 
		{
			dateRetrieved = latestDatesDAOImpl.getLatestDateWhileDeletingElectricityMeter(electricityMeterIDChosenToDelete);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeConnection();
		}
		
		return dateRetrieved;
	}
	
	@Override
	public Date getLatestDateWhileChangingLocalTerritoryElectricityMeterParentMeter(
			int electricityMeterID, int parentMeterIDOfUnwiredChild,
			int parentElectricityMeterIDThatReplacesTheOldOne) 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		LatestDatesDAO latestDatesDAOImpl = daoFactory.getLatestDatesDAOImpl(dbConnection);
		Date dateRetrieved = null;
		
		try 
		{
			dateRetrieved = latestDatesDAOImpl.getLatestDateWhileChangingLocalTerritoryElectricityMeterParentMeter(electricityMeterID,
					parentMeterIDOfUnwiredChild, parentElectricityMeterIDThatReplacesTheOldOne);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeConnection();
		}
		
		return dateRetrieved;
	}
	
	@Override
	public Date getLatestDateOfSubstationMainElectricityMeterToBeReplaced(
			int electricityMeterID) 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		LatestDatesDAO latestDatesDAOImpl = daoFactory.getLatestDatesDAOImpl(dbConnection);
		Date dateRetrieved = null;
		
		try 
		{
			dateRetrieved = latestDatesDAOImpl.getLatestDateOfSubstationMainElectricityMeterToBeReplaced(electricityMeterID);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeConnection();
		}
		
		return dateRetrieved;
	}
	
	@Override
	public Date getElectricityMeterLatestReadingDate(int electricityMeterID) 
	{
		Connection dbConnection = ConnectionManager.getInstance().getConnection();
		LatestDatesDAO latestDatesDAOImpl = daoFactory.getLatestDatesDAOImpl(dbConnection);
		Date dateRetrieved = null;
		
		try 
		{
			dateRetrieved = latestDatesDAOImpl.getElectricityMeterLatestReadingDate(electricityMeterID);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeConnection();
		}
		
		return dateRetrieved;
	}
}
